package com.polaris.live.adapter.level

import android.view.ViewGroup
import android.widget.TextView
import com.bumptech.glide.Glide
import com.hjq.shape.view.ShapeImageView
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.dp
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.resp.back_resp.RichCharmSizeBean
import com.polaris.live.resp.back_resp.UserRichCharmLevelRewardExplainVo
import com.polaris.live.utils.image.ImageUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * MeCharmRichBannerAdapter
 *
 * @author Created by 半仙 on 2024/2/1/001 18:56
 */
class MeCharmRichBannerAdapter : BaseBannerAdapter<UserRichCharmLevelRewardExplainVo>() {
    override fun bindData(
        holder: BaseViewHolder<UserRichCharmLevelRewardExplainVo>?,
        data: UserRichCharmLevelRewardExplainVo?,
        position: Int,
        pageSize: Int,
    ) {
        holder?.apply {
            val image = findViewById<ShapeImageView>(R.id.iv_level_image)
            val title = findViewById<TextView>(R.id.iv_level_title)
            val detail = findViewById<TextView>(R.id.iv_level_detail)
            title.text = data?.title
            detail.text = data?.content
            val richCharmSize = data?.ext?.let { JsonUtils.fromJson<RichCharmSizeBean>(it) }
            if (richCharmSize != null) {
                val layoutParams = image.layoutParams as ViewGroup.LayoutParams
                val (showHeight, showWidth) = if (richCharmSize.height != null && richCharmSize.width != null) {
                    (richCharmSize.height.toInt() / 3).autoDp to
                            (richCharmSize.width.toInt() / 3).autoDp
                } else {
                    100.dp to 100.dp
                }
                layoutParams.height = showHeight
                layoutParams.width = showWidth
                image.layoutParams = layoutParams
            }
            Glide.with(image).load(ImageUtils.imgPath(data?.photoUrl)).into(image)
        }
    }

    override fun getLayoutId(viewType: Int) = R.layout.adapter_charm_rich_banner
}